﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.Geography
{
    public partial class TrustRegionDetails : System.Web.UI.Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCountries();
                InitSaveMode();

            }
        }

        /// <summary>
        /// Inits the save mode.
        /// </summary>
        private void InitSaveMode()
        {
            int trustRegionId;
            if (int.TryParse(Request.Params["id"], out trustRegionId))
            {
                var trustRegion = TrustRegionsBO.GetTrustRegionById(trustRegionId);
                if (trustRegion != null)
                {
                    ButtonSave.CommandName = "edit";
                    ButtonInactive.Visible = trustRegion.IsActive;

                    LoadTrustRegions(trustRegion);

                    DetailsPanel02.Visible = true;
                    LinkDetails02.Visible = true;
                    TrustDistrictListFrame.TrustRegionId = trustRegion.TrustRegionId;
                }
            }
        }

        /// <summary>
        /// Loads the trust regions.
        /// </summary>
        /// <param name="trustRegion">The trust region.</param>
        private void LoadTrustRegions(TrustRegion trustRegion)
        {
            // Hidden fields
            HiddenFieldTrustRegionId.Value = trustRegion.TrustRegionId.ToString(CultureInfo.CurrentCulture);
            HiddenFieldTrustRegionName.Value = trustRegion.TrustRegionName;

            // Textbox
            TextBoxTrustRegionName.Text = trustRegion.TrustRegionName;
            TextBoxDescription.Text = trustRegion.Description;

            // Dropdownlist
            DropDownListNationCounty.SelectedValue = trustRegion.Country.CountryId;
        }

        /// <summary>
        /// Handles the Command event of the ButtonInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void ButtonInactive_Command(object sender, CommandEventArgs e)
        {
            int trustRegionId;
            if (int.TryParse(HiddenFieldTrustRegionId.Value, out trustRegionId))
            {
                TrustRegionsBO.SetTrustRegionStatus(trustRegionId, false);
                ButtonInactive.Visible = false;
            }
        }

        protected void ButtonSave_Command(object sender, CommandEventArgs e)
        {
            var trustRegion = new TrustRegion();
            trustRegion.CountryId = DropDownListNationCounty.SelectedValue;
            trustRegion.TrustRegionName = TextBoxTrustRegionName.Text;
            trustRegion.Description = TextBoxDescription.Text;
            trustRegion.IsActive = true;

            if (!HiddenFieldTrustRegionName.Value.Equals(trustRegion.TrustRegionName) &&
                TrustRegionsBO.CheckTrustRegionName(trustRegion.TrustRegionName))
            {
                LabelCheckName.Text = "Trust Region name must be unique";
                return;
            }

            if (e.CommandName.Equals("edit"))
            {
                trustRegion.TrustRegionId = int.Parse(HiddenFieldTrustRegionId.Value, CultureInfo.CurrentCulture);
            }

            bool result = e.CommandName.Equals("new")
                              ? TrustRegionsBO.InsertTrustRegion(trustRegion)
                              : TrustRegionsBO.EditTrustRegion(trustRegion);
            if (result)
            {
                Response.Redirect("~/Geography/TrustRegionList.aspx");
            }
            else
            {
                DialogBox.Error("Save Trust Region successfully .");
            }
        }

        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            int trustRegionId;
            if (int.TryParse(HiddenFieldTrustRegionId.Value, out trustRegionId))
            {
                Response.Redirect("~/Geography/TrustRegionList.aspx");
            }
        }
        private void LoadCountries()
        {
            DropDownListNationCounty.DataSource = CountryBO.SelectCountries();
            DropDownListNationCounty.DataBind();
        }
    }
}